#用对象存储信息  使用面向对象存储信息
#开户 查询 锁定 解锁 转账 退出 存款 取款
class Account:
    def __init__(self, name, id_number, password, balance=0):
        self.name = name
        self.id_number = id_number
        self.password = password
        self.balance = balance
        self.locked = False

    def deposit(self, amount):
        if self.locked:
            return False, "账户已锁定，无法存款"
        self.balance += amount
        return True, f"存款成功，当前余额：{self.balance}"

    def withdraw(self, amount):
        if self.locked:
            return False, "账户已锁定，无法取款"
        if amount > self.balance:
            return False, "余额不足"
        self.balance -= amount
        return True, f"取款成功，当前余额：{self.balance}"

    def show_info(self):
        lock_state = "锁定" if self.locked else "正常"
        return f"姓名: {self.name}, 身份证: {self.id_number}, 余额: {self.balance}, 状态: {lock_state}"


class Bank:
    def __init__(self):
        self.accounts = {}

    def create_account(self):
        name = input("请输入姓名：")
        id_number = input("请输入身份证号：")
        password = input("请输入密码：")

        if id_number in self.accounts:
            print("该身份证已开户")
            return

        self.accounts[id_number] = Account(name, id_number, password)
        print("开户成功！")

    def query(self):
        id_number, account = self._login()
        if account:
            print(account.show_info())

    def deposit(self):
        id_number, account = self._login()
        if account:
            amount = float(input("请输入存款金额："))
            ok, msg = account.deposit(amount)
            print(msg)

    def withdraw(self):
        id_number, account = self._login()
        if account:
            amount = float(input("请输入取款金额："))
            ok, msg = account.withdraw(amount)
            print(msg)

    def lock(self):
        id_number = input("请输入要锁定的身份证号：")
        if id_number in self.accounts:
            self.accounts[id_number].locked = True
            print("账户已锁定")
        else:
            print("无此账户")

    def unlock(self):
        id_number = input("请输入要解锁的身份证号：")
        if id_number in self.accounts:
            self.accounts[id_number].locked = False
            print("账户已解锁")
        else:
            print("无此账户")

    def transfer(self):
        print("=== 转账操作 ===")
        from_id, from_acc = self._login()
        if not from_acc:
            return

        to_id = input("请输入收款人身份证号：")
        if to_id not in self.accounts:
            print("收款账户不存在")
            return

        amount = float(input("请输入转账金额："))

        ok, msg = from_acc.withdraw(amount)
        if not ok:
            print(msg)
            return

        self.accounts[to_id].deposit(amount)
        print("转账成功")

    def _login(self):
        id_number = input("请输入身份证号：")
        if id_number not in self.accounts:
            print("无此账户")
            return None, None

        account = self.accounts[id_number]

        if account.locked:
            print("账户已锁定")
            return None, None

        password = input("请输入密码：")
        if password != account.password:
            print("密码错误")
            return None, None

        return id_number, account

    def run(self):
        while True:
            print("\n==== 银行系统 ====")
            print("1. 开户")
            print("2. 查询")
            print("3. 锁定账户")
            print("4. 解锁账户")
            print("5. 转账")
            print("6. 存款")
            print("7. 取款")
            print("8. 退出系统")
            choice = input("请选择功能：")

            if choice == "1":
                self.create_account()
            elif choice == "2":
                self.query()
            elif choice == "3":
                self.lock()
            elif choice == "4":
                self.unlock()
            elif choice == "5":
                self.transfer()
            elif choice == "6":
                self.deposit()
            elif choice == "7":
                self.withdraw()
            elif choice == "8":
                print("已退出系统")
                break
            else:
                print("输入错误，请重新选择")


if __name__ == '__main__':
    bank = Bank()
    bank.run()
